package com.atgui.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atgui.yygh.model.hosp.Department;
import com.atgui.yygh.repository.DepartmentRepository;
import com.atgui.yygh.service.DepartmentService;
import com.atgui.yygh.vo.hosp.DepartmentQueryVo;
import com.atgui.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * zz: wzh
 * time 2022 09 02 20 43
 **/
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        String jsonString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(jsonString, Department.class);

        Department departmentExists =departmentRepository.
                getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //如果里面有,就更新时间
        if (departmentExists!=null){
            departmentExists.setUpdateTime(new Date());
            departmentExists.setIsDeleted(0);
            departmentRepository.save(departmentExists);
        }else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Pageable pageable= PageRequest.of(page-1,limit);
        Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);
        ExampleMatcher exampleMatcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);
        Page<Department> departments = departmentRepository.findAll(example, pageable);
        return departments;
    }

    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDepTree(String hoscode) {
        //先创建一个list集合
        List<DepartmentVo> result =new ArrayList<>();

        Department departmentQuery=new Department();
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);
        //根据大科室编号bigcode分组,获取大科室下的小科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //遍历map
        for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()){
            //大科室编号
            String bigCode = entry.getKey();
            //大科室编号下的所有数据
            List<Department> departments=entry.getValue();
            //封装大科室
            DepartmentVo departmentVo1=new DepartmentVo();
            departmentVo1.setDepcode(bigCode);
            departmentVo1.setDepname(departments.get(0).getBigname());
            //封装小科室
            List<DepartmentVo> children= new ArrayList<>();
            for (Department department1 : departments) {
                DepartmentVo departmentVo=new DepartmentVo();
                departmentVo.setDepname(department1.getDepname());
                departmentVo.setDepcode(department1.getDepcode());
                //封装到list中
                children.add(departmentVo);
            }
            //把小科室的list放到children中去
            departmentVo1.setChildren(children);
            //放到result中
            result.add(departmentVo1);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
            return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
     return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
